package com.wimcorp.magic.host;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Host {

	public static void main(String[] args) {

		ApplicationContext appContext = new ClassPathXmlApplicationContext(new String[] {
				"applicationContext-magic-the-androiding-host.xml", "applicationContext-magic-the-androiding.xml" });

		try {
			String name = null;
			while (name == null || name.length() == 0) {
				System.out.println("Enter game name");
				BufferedReader bufferRead = new BufferedReader(new InputStreamReader(System.in));
				name = bufferRead.readLine();
				if (name.length() > 0) {
					Game game = (Game) appContext.getBean("game");// new Game();
					Registry registry = LocateRegistry.createRegistry(6123);
					registry.rebind(name, game);
					System.out.println("Hosting game : " + name);
				} else {
					System.out.println("Name should not be emtpy. ");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
